<template>
  <div class="activity-list">
    <div class="screen">
      <span>活动时间:</span>
      <div class="screen-item">
        <el-date-picker
          v-model="activityTime"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        >
        </el-date-picker>
      </div>

      <span>添加时间:</span>
      <div class="screen-item">
        <el-date-picker
          v-model="exchangeTime"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        >
        </el-date-picker>
      </div>

      <span>活动状态:</span>
      <div class="screen-item">
        <el-select v-model="status">
          <el-option
            v-for="item in statusList"
            :key="item.value"
            :value="item.value"
            :label="item.label"
          ></el-option>
        </el-select>
      </div>

      <el-button type="primary" @click="search">搜索</el-button>
      <el-button type="primary" @click="reset">重置</el-button>
    </div>
    <div class="table" ref="tablebox">
      <el-table :data="list" border :height="oHeight" style="width: 100%">
        <el-table-column prop="name" label="活动时间" align="center">
          <template slot-scope="scope"
            >{{ scope.row.start_time }} 至 {{ scope.row.end_time }}</template
          >
        </el-table-column>
        <el-table-column
          prop="create_time"
          label="添加时间"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="status_desc"
          label="状态"
          align="center"
        ></el-table-column>
        <el-table-column prop="name" label="操作" align="center">
          <template slot-scope="scope">
            <!-- <span class="edit" @click="edit(scope.row)">{{
              scope.row.status == "0" ? "编辑" : "查看"
            }}</span> -->
            <template v-if="scope.row.status == 0">
              <el-button type="text" @click="edit(scope.row)" v-if="scope.row.status == 0">编辑</el-button>
              <el-button type="text" @click="handleActivity('delete', scope.row.activity_id)">删除</el-button>
            </template>
            <el-button type="text" @click="handleActivity('update', scope.row.activity_id)" v-else-if="scope.row.status == 1">下架</el-button>
            <template v-else>
              <el-button type="text" @click="edit(scope.row)">查看</el-button>
              <el-button type="text" @click="edit(scope.row, true)">复制</el-button>
            </template>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="paging">
      <el-button type="primary" @click="add">新增活动</el-button>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="totalCount"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      list: [],
      statusList: [
        { label: "未开启", value: "0" },
        { label: "进行中", value: "1" },
        { label: "已结束", value: "2" },
      ],
      activityTime: [],
      exchangeTime: [],
      status: "",
      page: 1,
      pageSize: 20,
      totalCount: 0, // 总页数
      currentPage: 1,
      oHeight: 0,
    };
  },
  created() {
    this.getActivetyList();
  },
  mounted() {
    let h = this.$refs.tablebox.offsetHeight;
    this.$nextTick(() => {
      this.oHeight = h;
    });
  },
  methods: {
    async handleActivity(type = 'update', activity_id) {
      try {
        let title = '下架当前活动'
        let url = 'admin/pointprod/index.php?act=pt&op=ptOffline'
        if (type === 'delete') {
          title = '删除当前活动'
          url = 'admin/pointprod/index.php?act=pt&op=deletePt'
        }
        await this.$confirm(title + ', 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
        const res = await this.$http.post(url, { activity_id })
        if (res.code == 1) {
          this.getActivetyList()
        } else {
          this.$message.error(res.msg)
        }
      } catch (error) {
        error !== 'cancel' && this.$message.error(`${error}`)
      }
    },
    getActivetyList() {
      let data = {};
      if (this.activityTime && this.activityTime.length > 0) {
        data.start_time = this.activityTime[0] + "00:00:00";
        data.end_time = this.activityTime[1] + "23:59:59";
      }
      if (this.exchangeTime && this.exchangeTime.length > 0) {
        data.create_time_start = this.exchangeTime[0] + "00:00:00";
        data.create_time_end = this.exchangeTime[1] + "23:59:59";
      }
      if (this.status != "") {
        data.status = this.status;
      }
      data.page = this.page;
      data.pageSize = this.pageSize;
      this.$http
        .get("/admin/pointprod/index.php?act=pt&op=ptActivityList", {
          params: data,
        })
        .then((res) => {
          if (res.code == 1) {
            this.list = res.data.list;
            this.totalCount = res.data.totalCount;
          } else {
            this.$message.warning(res.msg);
          }
        });
    },
    handleSizeChange(val) {
      this.pageSize = val;
      this.getActivetyList();
    },
    handleCurrentChange(val) {
      this.page = val;
      this.getActivetyList();
    },
    // 搜索
    search() {
      this.getActivetyList();
    },
    // 重置
    reset() {
      this.activityTime = [];
      this.exchangeTime = [];
      this.status = "";
      this.page = 1;
      this.pageSize = 20;
      this.currentPage = 1;
      this.getActivetyList();
    },
    //   新增活动
    add() {
      this.$emit("addVibible", { status: "-1" });
    },
    edit(row, copy = false) {
      row.copy = copy
      this.$emit("addVibible", row);
    },
  },
};
</script>

<style lang="scss" scoped>
.activity-list {
  height: 94%;
  display: flex;
  flex-direction: column;
  .screen {
    padding: 10px;
    display: flex;
    flex-wrap: wrap;
    font-size: 14px;
    align-items: center;
    .screen-item {
      margin: 10px;
    }
  }
  .table {
    flex: 1;
    .edit {
      color: #409eff;
      cursor: pointer;
    }
    .tablebox {
      height: 100%;
    }
  }
  .paging {
    display: flex;
    justify-content: space-between;
    padding-top: 10px;
    height: 40px;
  }
}
</style>
